using IQIGame.Onigao.GamePlay;

namespace IQIGame.Onigao.Logic
{
    /// <summary>
    /// 取消寻路
    /// 参数组1：1.实体tag或mcid，2.是否触发寻路结束的服务，3.是否瞬移到终点
    /// </summary>
    public class ExecCancelNavigation : BaseServiceExecution
    {
        public override void Execute(LevelRegion region, LevelPlayer player)
        {
            var config = _config as ExecParamCancelNavigation;
            if (!config.tagOrMcid.TryGetValue(triggerEntity, out int tagOrMcid))
            {
                LogicLog.LogError("无法获取到tagOrMcid.");
                return;
            }
            bool isTriggerService = config.isTriggerService;
            bool isTeleportToFinalDestination = config.isTeleportToFinalDestination;

            LevelEntity targetEntity = tagOrMcid == 0 ? triggerEntity : region.GetEntityByTagOrMcid(tagOrMcid);

            if (targetEntity == null)
            {
                LogicLog.LogError($"没有找到实体，tagOrMcid：{tagOrMcid}");
                return;
            }

            LgNpcNavAssistantComponent navComponent = targetEntity.GetComponent<LgNpcNavAssistantComponent>();
            if (navComponent == null || navComponent.navIsComplete)
            {
                return;
            }
            if (navComponent.combinedWithOtherComponent)
            {
                LogicLog.LogError("停止寻路失败，当前寻路组件并非自我管理，请通过其他功能停止，如循环表演（LgEntityRoutineActComponent）");
                return;
            }
            if (isTeleportToFinalDestination)
            {
                navComponent.StopAndDestroy(isTriggerService, navComponent.finalDestination);
            }
            else
            {
                navComponent.StopAndDestroy(isTriggerService, targetEntity.position);
            }
        }
    }
}
